//
//  CancelOldViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15/12/17.
//  Copyright © 2015年 RuiBen. All rights reserved.
//

#import "CancelOldViewController.h"
#import "GuideListTableViewCell.h"

@interface CancelOldViewController ()
{
    NSMutableArray *guideCodeArray;
    NSMutableArray *guideNameArray;
    NSString *guideCodeStr;
    NSString *memberCodeStr;
}
@end

@implementation CancelOldViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    guideCodeArray=[NSMutableArray array];
    guideNameArray=[NSMutableArray array];
    _textView.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1] CGColor];
    _textView.layer.borderWidth= 1.0f;
    _textView.layer.cornerRadius=3;
    
    guideCodeStr=[Global getGuideCodeStr];
    _guideNameLabel.text=[Global getGuideNameStr];
    [Global resizeForLable:_guideNameLabel WithText:_guideNameLabel.text parentView:self.view];
    CGRect frameK=_guideNameLabel.frame;
    frameK.origin.x=_guideNameLabel.frame.origin.x-frameK.size.width-5;
    _guideNameLabel.frame=frameK;
    
    [_guideListBtn addTarget:self action:@selector(selectGuideBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_alphaButton setBackgroundColor:[Global parseColor:WESTORE_TRANSCLUCENT]];
    [_alphaButton addTarget:self action:@selector(alphaViewClick) forControlEvents:UIControlEventTouchUpInside];
      [self requestDetail];

}
-(void)requestDetail
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:@"OldMaterial" forKey:@"DocType"];
        [params setObject:_DocNumStr forKey:@"DocNum"];
        
        
        [HttpRequest requestData:@"Guide/GatheringDetail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                   
                  
                    NSString *nameStr=@"";
                    NSString *phoneStr=@"";
                    if ([severDataDict.dataDict objectForKey:@"MemberName"]!=[NSNull null]) {
                        nameStr=[severDataDict.dataDict objectForKey:@"MemberName"];
                    }
                    if ([severDataDict.dataDict objectForKey:@"Phone"]!=[NSNull null]) {
                        phoneStr =[severDataDict.dataDict objectForKey:@"Phone"];
                    }
                    
                    // memberCodeStr=[memberCodeArray objectAtIndex:indexPath.row];
                    //  NSString *integralStr=[memberIntegralArray objectAtIndex:indexPath.row];
                    _memberAndPhoneLabel.text=[NSString stringWithFormat:@"%@(%@)",nameStr,phoneStr];
                    memberCodeStr=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"MemberCode"]];
                    if (memberCodeStr.length>0 ) {
                        _memberNoteLabel.hidden=YES;
                    }
                    else
                    {
                        _memberNoteLabel.hidden=NO;
                    }
                    if (_memberAndPhoneLabel.text.length>0) {
                        
                        //   [self resizeLabel:_memberAndPhoneLabel];
                        [Global resizeForLable:_memberAndPhoneLabel WithText:_memberAndPhoneLabel.text parentView:self.view];
                        
                        if ([severDataDict.dataDict objectForKey:@"Grade"]!= [NSNull null])
                        {
                            NSString *gradeStr=[severDataDict.dataDict objectForKey:@"Grade"];
                            _memberGradeLabel.text=gradeStr;
                            _memberGradeTagLabel.hidden=NO;
                            _memberGradeLabel.hidden=NO;
                            //  [self resizeLabel:_memberGradeLabel];
                            [Global resizeForLable:_memberGradeLabel WithText:_memberGradeLabel.text parentView:self.view];
                            //_memberGradeLabel.text=[severDataDict.dataDict objectForKey:@"Grade"];
                        }
                        
                        CGRect frame=_memberGradeTagLabel.frame;
                        frame.origin.x=_memberAndPhoneLabel.frame.origin.x+_memberAndPhoneLabel.frame.size.width+12;
                        _memberGradeTagLabel.frame=frame;
                        
                        CGRect frame1=_memberGradeLabel.frame;
                        frame1.origin.x=_memberGradeTagLabel.frame.origin.x+_memberGradeTagLabel.frame.size.width+2;
                        _memberGradeLabel.frame=frame1;
                        
                        CGRect frame2=_memberIntegralTagLabel.frame;
                        frame2.origin.x=_memberGradeLabel.frame.origin.x+_memberGradeLabel.frame.size.width+12;
                        _memberIntegralTagLabel.frame=frame2;
                        
                        CGRect frame3=_memberIntegralLabel.frame;
                        frame3.origin.x=_memberIntegralTagLabel.frame.origin.x+_memberIntegralTagLabel.frame.size.width+2;
                        _memberIntegralLabel.frame=frame3;
                    }
                    
                    if ([severDataDict.dataDict objectForKey:@"GuideName"]!=[NSNull null]) {
                        _guideNameLabel.text=[severDataDict.dataDict objectForKey:@"GuideName"];
                        // [self resizeLabel:_guideNameLabel];
                        
                        [Global resizeForLable:_guideNameLabel WithText:_guideNameLabel.text parentView:self.view];
                        CGRect frameK=_guideNameLabel.frame;
                        frameK.origin.x=_guideArrowImage.frame.origin.x-frameK.size.width-5;
                        _guideNameLabel.frame=frameK;
                    }
                    
                    
                    NSString *numStr=[severDataDict.dataDict objectForKey:@"DocNum"];
                    _numLabel.text=numStr;
                    // [self resizeLabel:_numLabel];
                    [Global resizeForLable:_numLabel WithText:_numLabel.text parentView:self.view];
                    
                    CGRect framep=_statusLabel.frame;
                    framep.origin.x=_numLabel.frame.origin.x+_numLabel.frame.size.width+2;
                    _statusLabel.frame=framep;
                    
                NSString *statusStr=[severDataDict.dataDict objectForKey:@"DocStatus"];
                    
                _statusLabel.text=[NSString stringWithFormat:@"【%@】",statusStr];
                [_statusLabel setTextColor:[Global parseStatusColor:statusStr] ];
                [Global resizeForLable:_statusLabel WithText:_statusLabel.text parentView:self.view];
                    
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        
                        [Global loginViewController:self noteStr:requestResultStr];
                        ///  [Global loginViewController:self];
                    }
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
}

-(void)selectGuideBtnClick:(UIButton *)sender
{
    if (_guideListViewContainer.frame.size.height>1) {
        _guideListViewContainer.hidden=YES;
        [UIView animateWithDuration:0.5 animations:^{
            
            CGRect frame=_guideListViewContainer.frame;
            
            frame.size.height=1;
            [_guideListViewContainer setFrame:frame];
            
        } completion:^(BOOL finished){
            
        }];
        
    }
    else
    {
        [self requestGuideList];
        _guideListViewContainer.hidden=NO;
        CGRect frame1=_guideListViewContainer.frame;
        frame1.origin.y=111;
        [_guideListViewContainer setFrame:frame1];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            CGRect frame=_guideListViewContainer.frame;
            
            frame.size.height=657;
            [_guideListViewContainer setFrame:frame];
            
        } completion:^(BOOL finished){
            
        }];
        
    }
    
}
-(void)requestGuideList
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [HttpRequest requestData:@"Guide/GuideList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    [guideCodeArray removeAllObjects];
                    [guideNameArray removeAllObjects];
                    NSMutableArray *mGuidesArray=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (int i=0; i<mGuidesArray.count; i++) {
                        [guideCodeArray addObject:[[mGuidesArray objectAtIndex:i] objectForKey:@"UserCode"]];
                        [guideNameArray addObject:[[mGuidesArray objectAtIndex:i] objectForKey:@"UserName"]];
                    }
                    // NSLog(@"guideCodeArray==%@",guideCodeArray);
                    //
                    
                    [_guideListTableView reloadData];
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return guideCodeArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 55.0f;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * const  GuideListIdentifier = @"GuideListCellIdentifier";
    GuideListTableViewCell *cell=( GuideListTableViewCell *)[tableView dequeueReusableCellWithIdentifier: GuideListIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"GuideListTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (guideNameArray.count>0) {
        cell.guideNameLabel.text=[guideNameArray objectAtIndex:indexPath.row];
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // if ([guideOrAllocateStr isEqualToString:@"GUIDE"]) {
    
    guideCodeStr=[guideCodeArray objectAtIndex:indexPath.row];
    
    _guideListViewContainer.hidden=YES;
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame=_guideListViewContainer.frame;
        
        frame.size.height=1;
        [_guideListViewContainer setFrame:frame];
        
    } completion:^(BOOL finished){
        
    }];
    _guideNameLabel.text=[guideNameArray objectAtIndex:indexPath.row];
    // }
    // [self resizeLabel1:_guideNameLabel];
    [Global resizeForLable:_guideNameLabel WithText:_guideNameLabel.text parentView:self.view];
    CGRect frameK=_guideNameLabel.frame;
    frameK.origin.x=_guideArrowImage.frame.origin.x-frameK.size.width-5;
    _guideNameLabel.frame=frameK;
    
}


-(void)alphaViewClick
{
    if (self.view!=nil) {
        
        [self removeFromParentViewController];
        [self.view removeFromSuperview];
        
    }

}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView==_textView) {
        if (textView.text.length == 0) {
            _textViewLabel.hidden=NO;
            _textViewImage.hidden=NO;
        }
        else
        {
            _textViewImage.hidden=YES;
            _textViewLabel.hidden=YES;
            
        }
        
    }
}
- (IBAction)confirmBtnClick:(id)sender {
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        
        [params setObject:_DocNumStr forKey:@"DocNum"];
        [params setObject:guideCodeStr forKey:@"GuideCode"];
         [params setObject:@"OldMaterial" forKey:@"DocType"];
        
        [params setObject:_textView.text forKey:@"Remark"];
        
        [HttpRequest requestData:@"Guide/CancelDocument" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    if ([self.delegate respondsToSelector:@selector(confirmCancelOldSuccess)]) {
                        [self.delegate confirmCancelOldSuccess];
                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        
                        [Global loginViewController:self noteStr:requestResultStr];
                        ///  [Global loginViewController:self];
                    }
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }

}

@end
